//
//  ZMEmotionKeyBoarkToolBar.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/8/1.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

enum EmotionKeyBoardToolBarButtonType: Int {
    //最近表情
    case recent = 1000
    //默认表情
    case normal = 1001
    //emoji表情
    case emoji = 1002
    //lkh
    case ikh = 1003
    
}

class ZMEmotionKeyBoarkToolBar: UIStackView {

    //属性记录上一个被选中的按钮
    private var lastSelectedButton:UIButton?
    
    //闭包将按钮点击事件传递到外面
    var clickToolBarButtonClosure: ((EmotionKeyBoardToolBarButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //初始化UI
    private func setupUI(){
        
        self.axis = .horizontal
        self.distribution = .fillEqually
        
        
         addchildButton(title: "最近", imageName: "compose_emotion_table_left", type: EmotionKeyBoardToolBarButtonType.recent)
         addchildButton(title: "默认", imageName: "compose_emotion_table_mid", type: EmotionKeyBoardToolBarButtonType.normal)
         addchildButton(title: "Emoji", imageName: "compose_emotion_table_mid", type: EmotionKeyBoardToolBarButtonType.emoji)
         addchildButton(title: "浪小花", imageName: "compose_emotion_table_right", type: EmotionKeyBoardToolBarButtonType.ikh)
        
        
    }
    
    private func addchildButton(title:String,imageName:String,type:EmotionKeyBoardToolBarButtonType){
        
        let btn = UIButton()
        //设置btn  的tag  使用枚举原始值
        btn.tag = type.rawValue
        //设置按钮的属性
        btn.setTitle(title, for: UIControlState.normal)
        btn.setImage(UIImage(named:imageName), for: UIControlState.normal)
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.selected)
        btn.setBackgroundImage(UIImage(named:imageName+"_normal"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:imageName+"_selected"), for: UIControlState.selected)
        
        
        
        //添加点击事件
        btn.addTarget(self, action: #selector(toolBarButtonClick(sender:)), for: UIControlEvents.touchUpInside)
        //取消高亮
        btn.adjustsImageWhenHighlighted = false
        //添加控件
        self.addArrangedSubview(btn)
        
        
    }
    
    //MARK:- 按钮单击事件
    @objc private func toolBarButtonClick(sender:UIButton){
        
        if lastSelectedButton == sender{
            return
        }
        lastSelectedButton?.isSelected = false
        sender.isSelected = true
        
        lastSelectedButton = sender
        
        //  根据tag获取枚举
        let type = EmotionKeyBoardToolBarButtonType(rawValue: sender.tag)!

        //执行闭包
        clickToolBarButtonClosure?(type)
    }
    
    //  根据表情组数索引选中对应的按钮
    func selectButtonWith(buttonTag: Int) {
        //  根据tag获取对于的子视图, 提示：视图的tag不建议从0开始设置，因为如果tag是0那么获取的视图是当前视图自己
        let button = viewWithTag(buttonTag + 1000) as! UIButton
        //  判断是否是同一个按钮
        if button == lastSelectedButton {
            return
        }
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        
    }

}
